<template>
	<div class="tab-body">
		
		<div class="search">
			<el-form ref="form" :model="searchParams" label-width="80px">
				<el-row>
					<el-col :span="4">
						<el-form-item label="商品名称">
							<el-input v-model="searchParams.productName"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="4">
						<el-form-item label="活动类型">
							<el-input v-model="searchParams.activityType"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="5">
						<el-form-item label="活动状态">
							<el-select v-model="searchParams.status" placeholder="请选择活动状态">
								<el-option label="未开启" value="1"></el-option>
								<el-option label="进行中" value="2"></el-option>
								<el-option label="已结束" value="3"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="参与人数">
							<el-col :span="5">
								<el-input v-model="searchParams.least"></el-input>
							</el-col>
							<el-col class="line" :span="2">-</el-col>
							<el-col :span="5">
								<el-input v-model="searchParams.more"></el-input>
							</el-col>
						</el-form-item>
					</el-col>
					
					<div class="search-btn">
						<el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
						<el-button @click="clearSearchVal">清空</el-button>
					</div>
				</el-row>
			</el-form>
		</div>
		<div class="operator">
			<el-button type="primary" icon="el-icon-plus" @click="toSave()" v-has-permission-code-and="'000000000005'">添加活动</el-button>
			<el-button type="danger" icon="el-icon-delete" @click="batchRemove()" v-has-permission-code-and="'003000250010'">批量删除</el-button>
		</div>
		<div class="dataTable autoFlex">
			<qlh-table class="table" ref="orderTable" :data="listData.records" stripe border height="auto" @selection-change="selectionChange"
			v-loading="listLoading" :tableToolButton="tableToolButton">
				<qlh-table-column type="selection" width="55" fixed="left" ignored :editable="false"></qlh-table-column>
				<qlh-table-column prop="productName" label="商品名称"  width="80"></qlh-table-column>
				<qlh-table-column prop="storeName" label="店铺名称"  width="80"></qlh-table-column>
				<qlh-table-column prop="businessName" label="商家名称"  width="100"></qlh-table-column>
				<qlh-table-column prop="sum" label="活动数量" width="80"></qlh-table-column>
				<qlh-table-column prop="sendCount" label="售出数量" width="80"></qlh-table-column>
				<qlh-table-column prop="price" label="活动价格" width="80"></qlh-table-column>
				<qlh-table-column prop="startTime" label="开始时间" width="160"></qlh-table-column>
				<qlh-table-column prop="realTime" label="真实开始时间" width="160"></qlh-table-column>
				<qlh-table-column prop="endTime" label="结束时间" width="160"></qlh-table-column>
				<qlh-table-column prop="userCount" label="参与人数" width="80"></qlh-table-column>
				<qlh-table-column prop="end" label="启用" ignored :editable="false">
					<template slot-scope="scope">
						<el-switch v-model="scope.row.end" :active-value="true" :inactive-value="false" @change="change(scope.row.id,scope.row.status)" :disabled="scope.row.status == 3 ? true : false">
						</el-switch>
					</template>
				</qlh-table-column>
				<qlh-table-column prop="dictionary_status" label="活动状态" width="80"></qlh-table-column>
				<qlh-table-column label="操作" fixed="right" width="300" ignored :editable="false">
					<template slot-scope="scope">
						<div class="dataTable-operator">
							<el-button v-has-permission-code-and="'003000250005'" size="mini" icon="el-icon-search" @click="toDetail(scope.$index, scope.row)">编辑
							</el-button>
							<el-button v-has-permission-code-and="'003000250005'" size="mini" icon="el-icon-search" @click="toDetail(scope.$index, scope.row)">参与用户
							</el-button>
							<el-button v-has-permission-code-and="'003000250010'" size="mini" type="danger" icon="el-icon-delete" @click="remove(scope.$index, scope.row)">删除
							</el-button>
						</div>
					</template>
				</qlh-table-column>
			</qlh-table>
			<div class="pageBar">
				<el-pagination @size-change="sizeChange" @current-change="currentChange" :total="listData.total" :page-size="page.size"
				:current-page="page.current" :layout="this.$constant.page.layout" :page-sizes="this.$constant.page.pageSizes"></el-pagination>
			</div>
		</div>

		<edit ref="edit"></edit>
	</div>
</template>
<script>
	import edit from './edit.vue';
	import api from '@/api/tgb/activify/activify.js';
	import qlhTable from '@/components/qlhTable/qlhTable.vue';
	import qlhTableColumn from '@/components/qlhTable/qlhTableColumn.vue';
	export default {
		name: 'department',
		components: {
			edit,
			qlhTable,
			qlhTableColumn
		},
		data() {
			return {
				listData: [],
				listLoading: false,
				currentNode: {},
				tableLoading: true,
				editParameter: {},
				searchParams: {},
				page: new this.$constant.pageObj(),
				/**
				 * 表格自定义工具栏按钮
				 * name:按钮名
				 * icon:按钮图标
				 * func:按钮点击事件
				 */
				tableToolButton: [{
					name: "customButton",
					icon: "el-icon-arrow-down",
					func: this.customButton
				}],
			};
		},
		methods: {
			change(id,status){
				let newStatus = 1;
				if(status == 1){
					newStatus = 2;
				}
				if(status == 2){
					newStatus = 3;
				}
				let params = this.$utils.merger({"activityId":id,"status":newStatus})
				api.updateStatus(params,response => {
					this.list();
				})
				// console.log(id,status)
			},
			//获取数据
			list() {
				this.listLoading = true;
				let params = this.$utils.merger(this.searchParams, this.page);
				api.list(params, response => {
					this.listData = response.data;
					this.listLoading = false;
				})
			},
			//去添加
			toSave() {
				this.$refs.edit.open({}, '添加');
			},
			//去详情
			toDetail(index, row) {
				let detailParameter = {
					id: row.id
				};
				this.$refs.edit.open(detailParameter, '订单详情', true);
			},
			//删除
			remove(index, row) {
				let parameter = {
					ids: row.id
				}
				this.$utils.confirm.warning('提示', '确定删除吗？', () => {
					api.remove(
						parameter,
						response => {
							this.$utils.msg.success(response.msg);
							this.list();
						},
						response => {
							this.$utils.msg.warning(response.msg);
						}
					);
				});
			},
			//批量删除
			batchRemove() {
				if (!this.selected || this.selected.length == 0) {
					this.$utils.msg.info('请选择至少一行');
					return;
				}
				let parameter = {
					ids: this.selected
				};
				this.$utils.confirm.warning('提示', '确定删除吗？', () => {
					api.remove(parameter,
						response => {
							this.$utils.msg.success(response.msg);
							this.list();
						},
						response => {
							this.$utils.msg.warning(response.msg);
						}
					);
				});
			},
			handleSelect(key) {
				this.status = key
				this.list()
			},
			//搜索
			search() {
				this.list();
			},
			//清空搜索框
			clearSearchVal() {
				this.searchParams = {};
			},
			//改变选择项
			selectionChange(val) {
				//清空
				this.selected = [];
				for (let item of val) {
					this.selected.push(item.id);
				}
			},
			//改变每页显示数量
			sizeChange(val) {
				this.page.size = val;
				this.list();
			},
			//改变现在的页码
			currentChange(val) {
				this.page.current = val;
				this.list();
			},
		},
		mounted() {
			this.list();
		}
	};
</script>

<style lang="scss" scoped="scoped">
	@import '~common/custom/css/common.scss';
</style>
